import "reflect-metadata";
import { createConnection } from "typeorm";
import { User } from "./entity/User";
import { UserExtend } from "./entity/UserExtend";
import { Posts } from "./entity/Posts";
import { Tags } from "./entity/Tags";

// --------- part03------------
/** 
createConnection()
  .then(async (connection) => {
    // 新增
    // const user = new User();
    // user.username = "王五";
    // user.password = "123456";
    // await connection.manager.save(user);
    // console.log("新增成功", user);

    // 删除
    // const userRepository = await connection.getRepository(User);
    // const user = await userRepository.findOne(9);
    // await userRepository.remove(user);

    // 修改数据
    // const userRepository = await connection.getRepository(User);
    // const user = await userRepository.findOne(9);
    // console.log(user);
    // user.password = "23456";
    // await userRepository.save(user);

    // 查询
    const res = await connection.manager.find(User, { id: 10 });
    console.log("查询", res);
  })
  .catch((error) => console.log(error));
*/

// --------- part04------------
// 一对一
/** 
createConnection()
  .then(async (connection) => {
    // 新增
    // const user = new User();
    // user.username = "Emma";
    // user.password = "123456";

    // const userExtend = new UserExtend();
    // userExtend.mobile = "13162116763";
    // userExtend.address = "上海";
    // // 关联两个数据模型
    // userExtend.user = user;

    // const userRepository = connection.getRepository(User);
    // // 先保存用户表，因为他要提供主键出来
    // await userRepository.save(user);

    // const userExtendRepo = connection.getRepository(UserExtend);
    // await userExtendRepo.save(userExtend);
    // console.log("数据插入成功", user);

    // 正向查询  使用`relations`关联正向查询数据
    // const userRepo = connection.getRepository(User);
    // // userDetail 就是当前表中定义的字段
    // const result = await userRepo.find({ relations: ["userDetail"] });

    // 反向查询
    const extendRepo = connection.getRepository(UserExtend);
    const result = await extendRepo.find({ relations: ["user"] });
    console.log(result);
  })
  .catch((error) => console.log(error));
*/

// 一对多
/** 
createConnection()
  .then(async (connection) => {
    // 新增
    // const post1 = new Posts();
    // post1.title = "文章一";
    // post1.content = "文章一内容";

    // const post2 = new Posts();
    // post2.title = "文章二";
    // post2.content = "文章二内容";

    // // 创建用户
    // const user = new User();
    // user.username = "李四";
    // user.password = "123456";
    // user.posts = [post1, post2];

    // const userRepository = connection.getRepository(User);
    // const postsRepo = connection.getRepository(Posts);

    // await postsRepo.save(post1);
    // await postsRepo.save(post2);
    // await userRepository.save(user);

    // console.log("数据添加成功");

    // 正向查询  使用`relations`关联正向查询数据
    // const userRepo = connection.getRepository(User);
    // // posts 就是当前表中定义的字段
    // const result = await userRepo.find({ relations: ["posts"] });

    // 反向查询
    const postsRepo = connection.getRepository(Posts);
    const result = await postsRepo.find({ relations: ["user"] });
    console.log(result);
  })
  .catch((error) => console.log(error));
*/

// 多对多
createConnection()
  .then(async (connection) => {
    // 新增
    // const tag1 = new Tags();
    // tag1.name = "mysql";

    // const tag2 = new Tags();
    // tag2.name = "node";

    // const post1 = new Posts();
    // post1.title = "文章3";
    // post1.content = "文章一内容";
    // post1.tags = [tag1, tag2];

    // const post2 = new Posts();
    // post2.title = "文章4";
    // post2.content = "文章二内容";
    // post2.tags = [tag1];

    // // // 创建用户
    // const user = new User();
    // user.username = "李六";
    // user.password = "123456";
    // user.posts = [post1, post2];

    // const userRepository = connection.getRepository(User);
    // const postsRepo = connection.getRepository(Posts);
    // const tagsRepo = connection.getRepository(Tags);

    // await tagsRepo.save(tag1);
    // await tagsRepo.save(tag2);

    // await postsRepo.save(post1);
    // await postsRepo.save(post2);
    // await userRepository.save(user);

    // console.log("数据添加成功");

    //查询帖子一拥有的`tag`及用户信息
    const postsRepo = connection.getRepository(Posts);
    const result = await postsRepo.findOne({
      where: { id: 5 },
      relations: ["tags", "user"],
    });
    console.log(result);
  })
  .catch((error) => console.log(error));
